package com.nidu.demo.oauth2.ability;

import com.alibaba.cola.exception.BizException;
import com.nidu.demo.common.exception.ErrorCodeConstants;
import com.nidu.demo.oauth2.gateway.OAuth2ClientGateway;
import com.nidu.demo.oauth2.model.OAuth2Client;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
public class OAuth2ClientDomainService {

    private final OAuth2ClientGateway gateway;

    public void validateDuplicate(String clientCode, Long excludeId) {
        OAuth2Client client = gateway.getByClientCode(clientCode);
        if(client != null){
            if(excludeId == null){
                throw new BizException(ErrorCodeConstants.OAUTH2_CLIENT_DUPLICATE.getCode(), ErrorCodeConstants.OAUTH2_CLIENT_DUPLICATE.getMessage());
            } else if(!excludeId.equals(client.getId())){
                throw new BizException(ErrorCodeConstants.OAUTH2_CLIENT_DUPLICATE.getCode(), ErrorCodeConstants.OAUTH2_CLIENT_DUPLICATE.getMessage());
            }
        }
    }

    public OAuth2Client validateClient(String clientCode) {
        OAuth2Client client = gateway.getByClientCode(clientCode);
        if (client == null){
            throw new BizException(ErrorCodeConstants.OAUTH2_CLIENT_NOT_FOUND.getCode(), ErrorCodeConstants.OAUTH2_CLIENT_NOT_FOUND.getMessage());
        }
        client.isDeleted();
        client.isDisable();
        return client;
    }



}
